package org.darkforest.merchant.controller;

import net.sf.json.JSONArray;
import org.darkforest.common.services.StoreMsgService;
import org.darkforest.common.services.StoreService;
import org.darkforest.merchant.utils.JSONUtils;
import org.darkforest.model.*;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by valentina on 15/4/29.
 * 客服中心
 */
@Controller
@RequestMapping("callCenter")
public class CallCenterController {

    @Autowired
    private StoreService storeService;


    @Autowired
    private StoreMsgService storeMsgService;

    /**
     * 店铺客服设置
     * @param session
     * @return
     */
    @RequestMapping("index")
    public ModelAndView index(HttpSession session){
        StoreSellerVo sellerVo= (StoreSellerVo)session.getAttribute("sellerInfo");

        //查询店铺详细信息 根据店铺ID
        StoreWithBLOBs store = storeService.findStoreInfoByPrimary(sellerVo.getStoreId());
        ModelAndView res = new ModelAndView("callCenter/index");

        List<StorePresales> storePresalesList = new ArrayList<StorePresales>();
        List<StoreAftersales> storeAftersalesList = new ArrayList<StoreAftersales>();
        if(!("".equals(store.getStorePresales())||store.getStorePresales()==null)){
            storePresalesList  = JSONUtils.getJavaCollection(new StorePresales(), store.getStorePresales());
        }
        if(!("".equals(store.getStoreAftersales())||store.getStoreAftersales()==null)){
            storeAftersalesList= JSONUtils.getJavaCollection(new StoreAftersales(),store.getStoreAftersales());
        }
        res.addObject("store",store);
        res.addObject("storePresalesList",storePresalesList);
        res.addObject("storeAftersalesList",storeAftersalesList);
        return res;
    }


    /**
     * 保存客服设置
     * @param session
     * @param storeSales
     * @param working_time
     * @return
     */
    @RequestMapping(value = "updateCustomerServiceExecutive")
    public String updateCustomerServiceExecutive(HttpSession session,@ModelAttribute("storeSales") StoreSales storeSales,String working_time){
        StoreSellerVo sellerVo= (StoreSellerVo)session.getAttribute("sellerInfo");
        //根据店铺ID 获取店铺详细信息
        StoreWithBLOBs store = storeService.findStoreInfoByPrimary(sellerVo.getStoreId());
        //将售前信息转换成json字符串
        String storePresales =new JSONArray().fromObject(storeSales.getStorePresales()).toString();
        String storeAftersales = new JSONArray().fromObject(storeSales.getStoreAftersales()).toString();
        if(storeSales.getStorePresales()==null){
            storePresales="";
        }
        if(storeSales.getStoreAftersales()==null){
            storeAftersales="";
        }
        store.setStorePresales(storePresales);
        store.setStoreAftersales(storeAftersales);
        store.setStoreWorkingtime(working_time);
        storeService.updateStoreSettings(store);
        return "redirect: index";
    }


    /**
     * 系统消息列表
     * @param session
     * @return
     */
    @RequestMapping("system")
    public ModelAndView system(HttpSession session,@RequestParam(required = false, defaultValue = "1") int p){
        ModelAndView res = new ModelAndView("callCenter/system");
        StoreSellerVo sellerVo= (StoreSellerVo)session.getAttribute("sellerInfo");
        StoreMsg storeMsgs = new StoreMsg();
        storeMsgs.setStart(p);
        storeMsgs.setPageSize(5);
        storeMsgs.setStoreId(sellerVo.getStoreId());
        storeMsgs = storeMsgService.findByPage(storeMsgs);
//        List<StoreMsg> storeMsgs = storeMsgService.findStoreMsgByLimits(sellerVo.getStoreId());
        res.addObject("pageBean", storeMsgs);
        return res;
    }

    /**
     * 系统消息详细
     * @param smId
     * @return
     */
    @RequestMapping(value = "msgDetail")
    public ModelAndView msgDetail(String smId){
        ModelAndView res = new ModelAndView("callCenter/system_msg_detail");
        StoreMsg storeMsg = storeMsgService.selectByPrimaryKey(Integer.parseInt(smId));
        storeMsgService.updateReadTagBatch(new String[]{smId});
        res.addObject("msgDetail",storeMsg);
        return  res;
    }


    /**
     * 消息标识为已读
     * @param request
     * @return
     */
    @RequestMapping(value = "modifyReadTag")
    public @ResponseBody AjaxReturnData modifyReadTag(HttpServletRequest request){
        String[] smids = request.getParameterValues("smids");
        String currentPage = request.getParameter("p");
        storeMsgService.updateReadTagBatch(smids);
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        ajaxReturnData.setUrl("callCenter/system?p="+currentPage);
        ajaxReturnData.setMessage("标识成功");
        return ajaxReturnData;
    }

    /**
     * 批量删除系统消息
     * @param request
     * @return
     */
    @RequestMapping(value = "delMsg")
    public @ResponseBody
    AjaxReturnData delMsg(HttpServletRequest request){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        String[] smids = request.getParameterValues("smids");
        storeMsgService.deleteMsgBatch(smids, sellerVo.getStoreId());
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        ajaxReturnData.setUrl("callCenter/system");
        ajaxReturnData.setMessage("删除成功");
        return ajaxReturnData;

    }

    @RequestMapping("systemSetting")
    public String system_setting(){

        return "callCenter/system_setting";
    }

    @RequestMapping("history")
    public String imHistory(){

        return "callCenter/history";
    }
}
